#!/bin/bash
#--user config variables--
GROUP_NAME_R="group_r"
GROUP_NAME_W="group_w"
GROUP_NAME_X="group_x"
DIR_SYS_CONFIG="/etc/fpermission"
FILE_USER_R="users_r.list"
FILE_USER_W="users_w.list"
FILE_USER_X="users_x.list"

FILE_DEST=""

#--system auto config variables(don't modify!)--
SUDO=""

function check_root_permisson()
{
    if [ ${UID} -eq 0 ]; then
        SUDO=""
    else
        SUDO=sudo
    fi
}

function check_users_config_file()
{
    if [ -d ${DIR_SYS_CONFIG} ]; then
        #if [ -f `pwd`/users_*.list ]; then
            ${SUDO} cp -rf `pwd`/users_*.list ${DIR_SYS_CONFIG} 2>/dev/null
        #fi
    else
        ${SUDO} mkdir -p ${DIR_SYS_CONFIG}
        #if [ -f `pwd`/users_*.list ]; then
            ${SUDO} cp -rf `pwd`/users_*.list ${DIR_SYS_CONFIG} 2>/dev/null
        #fi
    fi
}

#参数:$1,组名
function group_create()
{
    GROUP_NAME="$1"
    ${SUDO} groupadd ${GROUP_NAME} 2>/dev/null
}

#参数:$1,组名
function group_delete()
{
    group_name="$1"
    ${SUDO} groupdel -f ${group_name} 2>/dev/null
}

#参数:$1,用户列表文件
#参数:$2,组名
function group_add_members()
{
    users_file="$1"
    group_name="$2"
    cat ${users_file} | xargs -i -d '\n' ${SUDO} usermod -a -G ${group_name} {} 2>/dev/null
}

#参数:$1,哪个组拥有可写权限
#参数:$2,文件名
function which_group_writable()
{
    group_writable="$1"
    ${SUDO} chown ${user_writable}:${group_writable} ${} 2>/dev/null
}

#功能:把用户加入可写组
#参数:$1,可写用户列表文件
#参数:$2,可写组名
#参数:$3,目标文件
function insert_permission2group()
{
    users_file="$1"
    group_name="$2"
    file_name="$3"

    # 修改文件所属组
    cat ${users_file} | xargs -i -d '\n' ${SUDO} chown -fR {}:${group_name} ${file_name} 2>/dev/null

    # 给组设置文件访问权限=切换到当前组+修改当前组访问权限
    ${SUDO} chown -fR :${GROUP_NAME_R} ${file_name} 2>/dev/null
    ${SUDO} chmod -fR g+r ${FILE_DEST} 2>/dev/null
    ${SUDO} chown -fR :${GROUP_NAME_W} ${file_name} 2>/dev/null
    ${SUDO} chmod -fR g+w ${FILE_DEST} 2>/dev/null
    ${SUDO} chown -fR :${GROUP_NAME_X} ${file_name} 2>/dev/null
    ${SUDO} chmod -fR g+x ${FILE_DEST} 2>/dev/null

    #cat ${DIR_SYS_CONFIG}/${FILE_USER_R} | xargs -i -d '\n' ${SUDO} chown -fR {}:${GROUP_NAME_R} ${file_name} 2>/dev/null
    #${SUDO} chmod -fR g+r ${FILE_DEST} 2>/dev/null
    #cat ${DIR_SYS_CONFIG}/${FILE_USER_W} | xargs -i -d '\n' ${SUDO} chown -fR {}:${GROUP_NAME_W} ${file_name} 2>/dev/null
    #${SUDO} chmod -fR g+w ${FILE_DEST} 2>/dev/null
    #cat ${DIR_SYS_CONFIG}/${FILE_USER_X} | xargs -i -d '\n' ${SUDO} chown -fR {}:${GROUP_NAME_X} ${file_name} 2>/dev/null
    #${SUDO} chmod -fR g+x ${FILE_DEST} 2>/dev/null

    #cat ${DIR_SYS_CONFIG}/${FILE_USER_R} | xargs -i -d '\n' ${SUDO} chown -fR {}:${GROUP_NAME_R} ${file_name} 2>/dev/null
    #${SUDO} chmod -fR g${OPTION}${PERMISSION} ${FILE_DEST} 2>/dev/null
    #cat ${DIR_SYS_CONFIG}/${FILE_USER_W} | xargs -i -d '\n' ${SUDO} chown -fR {}:${GROUP_NAME_W} ${file_name} 2>/dev/null
    #${SUDO} chmod -fR g${OPTION}${PERMISSION} ${FILE_DEST} 2>/dev/null
    #cat ${DIR_SYS_CONFIG}/${FILE_USER_X} | xargs -i -d '\n' ${SUDO} chown -fR {}:${GROUP_NAME_X} ${file_name} 2>/dev/null
    #${SUDO} chmod -fR g${OPTION}${PERMISSION} ${FILE_DEST} 2>/dev/null
}

if [ $# -eq 1 ]; then
    FILE_DEST="$1"

    # 判断用户权限,需要root用户权限
    check_root_permisson

    # 判断用户列表配置文件是否在/etc/fpermission目录下
    #check_users_config_file

    # 删除原来旧的组
    group_delete "${GROUP_NAME_R}"
    group_delete "${GROUP_NAME_W}"
    group_delete "${GROUP_NAME_X}"

    # 创建3个组:分别管理-rwx
    group_create "${GROUP_NAME_R}"
    group_create "${GROUP_NAME_W}"
    group_create "${GROUP_NAME_X}"

    # 把用户添加进组
    group_add_members "${DIR_SYS_CONFIG}/${FILE_USER_R}" "${GROUP_NAME_R}"
    group_add_members "${DIR_SYS_CONFIG}/${FILE_USER_W}" "${GROUP_NAME_W}"
    group_add_members "${DIR_SYS_CONFIG}/${FILE_USER_X}" "${GROUP_NAME_X}"

    ## 把目标文件的相应访问权限添加进指定组
    insert_permission2group "${DIR_SYS_CONFIG}/${FILE_USER_R}" "${GROUP_NAME_R}" "${FILE_DEST}"
    insert_permission2group "${DIR_SYS_CONFIG}/${FILE_USER_W}" "${GROUP_NAME_W}" "${FILE_DEST}"
    insert_permission2group "${DIR_SYS_CONFIG}/${FILE_USER_X}" "${GROUP_NAME_X}" "${FILE_DEST}"

    cat /etc/group | grep "group_" 2>/dev/null
    exit 0
else
    echo ">> Usage: $0 <dstfile>"
    echo ">> Example: $0 /home/hello.txt"
    cat /etc/group | grep "group_" 2>/dev/null
    exit -1
fi

